package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/role")
@RestController
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Role role) {
        try {
            roleService.add(role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Role role) {
        try {
            roleService.edit(role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result deleteId(Integer id) {
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPermissionsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_ADD_PERMISSION')")
    public Result findPermissionsByRoleId(Integer roleId) {
        try {
            List<Integer> list = roleService.findPermissionsByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/setPermissionsAndRoleId")
    @PreAuthorize("hasAuthority('ROLE_ADD_PERMISSION')")
    public Result setPermissionsAndRoleId(Integer[] permissionIds, Integer roleId) {
        if (roleId != -1) {
            roleService.setPermissionsAndRoleId(permissionIds, roleId);
            return new Result(true, MessageConstant.BIND_ROLE_PERMISSION_SUCCESS);
        } else {
            return new Result(false, MessageConstant.BIND_ROLE_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/findAll") // todo 需要添加权限
    @PreAuthorize("hasAnyAuthority('USER_ADD','USER_EDIT')")
    public Result findAll() {
        try {
            List<Role> roles = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/setMenusAndRoleId")
    public Result setMenusAndRoleId(Integer[] menuIds, Integer roleId) {
        try {
            roleService.setMenusAndRoleId(menuIds,roleId);
            return new Result(true,MessageConstant.SET_ROLE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.SET_ROLE_MENU_FAIL);
        }
    }
}
